package com.cn.onetrip.activity;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import com.cn.onetrip.activity.R;
import com.cn.onetrip.untility.SDCardUtils;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.AssetManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;

import android.widget.ProgressBar;
import android.widget.Toast;
import io.vov.vitamio.MediaPlayer;
import io.vov.vitamio.MediaPlayer.OnErrorListener;
import io.vov.vitamio.MediaPlayer.OnCompletionListener;
import io.vov.vitamio.MediaPlayer.OnInfoListener;
import io.vov.vitamio.MediaPlayer.OnPreparedListener;
import io.vov.vitamio.widget.MediaController;
import io.vov.vitamio.widget.VideoView;

public class ScenicPlayMovieActivity extends Activity {

	public static final String TAG = "VideoPlayer";
	private VideoView mVideoView;
	private long mPositionWhenPaused = -1;
	private int activityFlag = 0;
	private ProgressBar pb;
	private String videoUrl = "";
	private String HeadMovieString = "";
	private boolean isHeadMovie = true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		if (!io.vov.vitamio.LibsChecker.checkVitamioLibs(this))
			return;

		setContentView(R.layout.activity_test_media);
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

		HeadMovieString = SDCardUtils
				.getFlashPath(ScenicPlayMovieActivity.this)
				+ "OneTripMap/ocr/headMovie/MovieHeader.mp4";

		mVideoView = (VideoView) findViewById(R.id.videoview);
		pb = (ProgressBar) findViewById(R.id.pb_loading);
		pb.setVisibility(View.VISIBLE);

		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		videoUrl = bundle.getString("videoUrl");
		mVideoView.setVideoPath(HeadMovieString);
		mVideoView.setVideoQuality(MediaPlayer.VIDEOQUALITY_HIGH);
		mVideoView.setMediaController(new MediaController(this));

		mVideoView.setOnCompletionListener(new OnCompletionListener() {

			@Override
			public void onCompletion(MediaPlayer mp) {
				// mp.release();
				if (isHeadMovie) {
					isHeadMovie = false;
					mVideoView.setVideoPath(videoUrl);
				} else {
					activitySwap();
				}
			}
		});

		mVideoView.setOnPreparedListener(new OnPreparedListener() {

			@Override
			public void onPrepared(MediaPlayer mp) {
				// TODO Auto-generated method stub
				pb.setVisibility(View.GONE);
				mVideoView.start();

				// Toast.makeText(getApplicationContext(), "ok",
				// Toast.LENGTH_LONG).show();
			}
		});

		mVideoView.setOnInfoListener(new OnInfoListener() {
			@Override
			public boolean onInfo(MediaPlayer mp, int what, int extra) {
				// Log.d("onInfo: (%d, %d)", what, extra);

				if (what == MediaPlayer.MEDIA_INFO_BUFFERING_START) {
					mVideoView.pause();
					pb.setVisibility(View.VISIBLE);
				} else if (what == MediaPlayer.MEDIA_INFO_BUFFERING_END) {
					mVideoView.start();
					pb.setVisibility(View.GONE);
				}

				return true;
			}
		});

		mVideoView.setOnErrorListener(new OnErrorListener() {

			@Override
			public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
				return true;
			}
		});
	}

	@Override
	public void onPause() {
		// Stop video when the activity is pause.
		mPositionWhenPaused = mVideoView.getCurrentPosition();
		mVideoView.pause();

		super.onPause();
	}

	@Override
	public void onResume() {
		// Resume video player
		if (mPositionWhenPaused >= 0) {
			mVideoView.seekTo(mPositionWhenPaused);
			mPositionWhenPaused = -1;
			mVideoView.start();
		}

		super.onResume();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			activitySwap();
		}
		return super.onKeyDown(keyCode, event);
	}

	private void activitySwap() {
		finish();
	}

}
